package com.winbomb.simulife.impl;

import java.util.ArrayList;
import java.util.List;

import com.winbomb.simulife.World;
import com.winbomb.simulife.base.IJob;
import com.winbomb.simulife.base.IJobManager;
import com.winbomb.simulife.impl.jobs.MicrogleJob;

public class JobManager implements IJobManager {

	private static JobManager _instance;

	private static World world;
	private IJob[] allJobs;

	public JobManager(World paramWorld) {
		world = paramWorld;
		allJobs = new IJob[5];
		allJobs[0] = new MicrogleJob(world);
		allJobs[1] = new MicrogleJob(world);
		allJobs[2] = new MicrogleJob(world);
		allJobs[3] = new MicrogleJob(world);
		allJobs[4] = new MicrogleJob(world);
	}

	@Override
	public IJob[] getAvailableJobs() {
		List<IJob> offeredJobs = new ArrayList<IJob>();

		for (IJob job : allJobs) {
			if (job.offer()) {
				offeredJobs.add(job);
			}
		}

		return offeredJobs.toArray(new IJob[0]);
	}

	public synchronized static JobManager getInstance(World paramWorld) {
		if (_instance == null) {
			_instance = new JobManager(paramWorld);
		}

		return _instance;
	}
}
